package de.metalcon.gwt.shared;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gwt.user.client.rpc.IsSerializable;

public class ContentTransmitter implements IsSerializable {
	private short TTL = 20;
	private short receivedTime = 0;
	private String uniqueKey = "";

	HashMap<String, Object> pairs = null;
	HashMap<String, ArrayList<ContentTransmitter>> lists = null;

	/**
	 * Constructor without caching possibility
	 */
	public ContentTransmitter() {
	}

	/**
	 * Constructor with standard time to live setting for client side caching
	 * 
	 * @param uniqueKey
	 *            Key of this object for caching
	 */
	public ContentTransmitter(String uniqueKey) {
		this.uniqueKey = uniqueKey;
		this.TTL = 10;
	}

	/**
	 * Constructor with time to live variable for client side caching
	 * 
	 * @param uniqueKey
	 *            Key of this object for caching
	 * @param TTL
	 *            Time To Live in seconds for caching
	 */
	public ContentTransmitter(String uniqueKey, short TTL) {
		this.uniqueKey = uniqueKey;
		this.TTL = TTL;
	}

	/**
	 * 
	 * @return A unique key belonging to this object to be used for caching
	 */
	public String getUniqueKey() {
		return uniqueKey;
	}

	public boolean isDirty() {
		int now = receivedTime; // TODO: Woher bekomme ich aktuelle Zeit? Habe
								// gerade kein Inet!
		return now - receivedTime >= TTL;
	}

	public void setReceivedNow(short time) {
		this.receivedTime = time;
	}

	private void initializePairs() {
		if (pairs == null) {
			pairs = new HashMap<String, Object>();
		}
	}

	private void initializeLists() {
		if (lists == null) {
			lists = new HashMap<String, ArrayList<ContentTransmitter>>();
		}
	}

	public Set<Entry<String, Object>> getAllPairs() {
		return pairs.entrySet();
	}

	public void setString(String key, String value) {
		initializePairs();
		pairs.put(key, value);
	}

	public void setInt(String key, int value) {
		initializePairs();
		pairs.put(key, value);
	}

	public String getString(String key) {
		return (String) pairs.get(key);
	}

	public int getInt(String key) {
		return (Integer) pairs.get(key);
	}

	public void addError(String errorMessage) {
		ContentTransmitter error = new ContentTransmitter();
		error.setString("message", errorMessage);
		addListElement("errorMessages", error);
	}

	public LinkedList<String> getErrorMessages() {
		LinkedList<String> list = new LinkedList<String>();
		ArrayList<ContentTransmitter> errors = getList("errorMessages");
		if (errors == null) {
			return list;
		}
		for (ContentTransmitter error : errors) {
			list.add(error.getString("message"));
		}
		return list;
	}

	public void addListElement(String listKey, ContentTransmitter element) {
		initializeLists();
		ArrayList<ContentTransmitter> found = lists.get(listKey);
		if (found == null) {
			found = new ArrayList<ContentTransmitter>();
			lists.put(listKey, found);
		}
		found.add(element);
	}

	public ArrayList<ContentTransmitter> getList(String listKey) {
		initializeLists();
		return lists.get(listKey);
	}

	public Set<Entry<String, ArrayList<ContentTransmitter>>> getAllLists() {
		initializeLists();
		return lists.entrySet();
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		for (Map.Entry<String, Object> entry : pairs.entrySet()) {
			buffer.append(entry.getKey() + ":" + entry.getValue() + ",");
		}
		return buffer.toString();
	}
}
